﻿/// <reference path="jquery/jquery-1.4.1-vsdoc.js" />

var cachedAvatar = new Array();
var cachedOnlineStatus = new Array();

function Utility() {
}

Utility.ParseJsonDate = function (jsonDate) {
    var ms = jsonDate.slice(6, 19);
    return new Date(parseInt(ms));
}

Utility.DisplayAvatar = function (callback) {
    var uidList = new Array();
    $('.avtb').each(function () {
        var u = $(this).find('span.uid');
        if (u.length > 0) {
            var uid = u.attr('title');
            if (!uidList.contains(uid)) {
                uidList.push(uid);
            }
        }
    });
    var p = uidList.join(';');

    if (p == "") return;
    $.post(
        relRootPath + '/Services/UserServices.asmx/GetUserInfo',
        { uidList: p },
        function (d, s, x) {
            var list = eval($(d).text());
            $.each(list, function (k, v) {
                Utility.RenderAvatar(v);
            });
            if (undefined != callback)
                callback();
        });
}

Utility.RenderAvatar = function (val) {
    if (undefined != val) {
        var html = "<div class='avatarthumbcontainer textcenter' uid='" + val.UserId + "'><div class='thumbimgbox'><a rel='avatar' href='/Home/?id=" + val.UserId + "'><img title='" + val.NickName + "' alt='" + val.NickName + "' style='border:none;' src='" + val.Thumb + "'/></a></div>";
        html += "<div class='namebox'>";

        if (val.Online == 1) {
            html += "<img class='online' src='" + relRootPath + "/Content/images/icons/greendot.png' alt='在线' title='在线' />&nbsp;";
        }
        html += "<a rel='nickname' class='avtb-nn' href='/Home/?id=" + val.UserId + "'>" + val.NickName + '</a></div>';
        html += '</div>';

        $(".avtb>span[title=" + val.UserId + "]").parent().replaceWith(html);
    }
}

$(function () {
    Utility.DisplayAvatar();
    prm.add_endRequest(function (s, a) {
        Utility.DisplayAvatar();
    });
});

Array.prototype.contains = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) {
            return true;
        }
    }
    return false;
}